import { nanoid } from 'nanoid'
import axios from 'axios'
export default {
  namespaced: true,
  state: {
    personList: [
      {
        id: 123,
        name: '小明'
      }
    ]
  },
  mutations: {
    ADD_PERSON (state, value) {
      const newObj = {
        id: nanoid(),
        name: value
      }
      // 在数组前插入
      state.personList.unshift(newObj)
    }
  },
  actions: {
    async suiji (context) {
      // 获取随机句子
      const { data: msg } = await axios.get('https://api.uixsj.cn/hitokoto/get?type=social')
      // 调用mutations的方法添加
      context.commit('ADD_PERSON', msg)
    }
  },
  getters: {
    twoPlus (state) {
      return state.personList.length * 2
    }
  }
}
